package com.siwei.eqi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.siwei.eqi.mapper.IndexCalculationTaskMapper;
import com.siwei.eqi.pojo.entity.IndexCalculationSubTaskEntity;
import com.siwei.eqi.pojo.entity.IndexCalculationTaskEntity;
import com.siwei.eqi.service.IndexCalculationSubTaskService;
import com.siwei.eqi.service.IndexCalculationTaskService;
import com.siwei.eqi.util.RegionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class IndexCalculationTaskServiceImpl extends ServiceImpl<IndexCalculationTaskMapper, IndexCalculationTaskEntity> implements IndexCalculationTaskService {

    @Autowired
    private IndexCalculationSubTaskService indexCalculationSubTaskService;

    @Override
    public void addIndexCalculationTask(IndexCalculationTaskEntity entity) throws Exception {
        entity.setCalculationStatus("计算中");
        baseMapper.insert(entity);
        // 添加子任务
        IndexCalculationSubTaskEntity indexCalculationSubTaskEntity = new IndexCalculationSubTaskEntity();
        indexCalculationSubTaskEntity.setCalculationArea(entity.getCalculationArea());
        indexCalculationSubTaskEntity.setParentTaskId(entity.getTaskId());
        indexCalculationSubTaskEntity.setCalculationStatus(entity.getCalculationStatus());
        indexCalculationSubTaskService.addIndexCalculationSubTask(indexCalculationSubTaskEntity);


    }

    @Override
    public R getList(String taskName, String calculationYear, String calculationArea, Long count, Long size) {
        QueryWrapper<IndexCalculationTaskEntity> wrapper = new QueryWrapper<>();
        Page<IndexCalculationTaskEntity> page = new Page<>(count, size);
        wrapper.eq(!calculationYear.isEmpty(), "calculation_year", calculationYear);
        wrapper.eq(!calculationArea.isEmpty(), "calculation_area", calculationArea);
        wrapper.eq(!taskName.isEmpty(), "task_name", taskName);

        IPage<IndexCalculationTaskEntity> pages = baseMapper.selectPage(page, wrapper);
        pages.getRecords().stream().forEach(indexCalculationTaskEntity -> {
            List<String> city = null;
            try {
                city = RegionUtil.getCity(indexCalculationTaskEntity.getCalculationArea());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            indexCalculationTaskEntity.setCalculationArea(city.get(0));
        });

        return R.ok(pages);
    }

    @Override
    public List<String> getYear() {
        List<IndexCalculationTaskEntity> indexCalculationTaskEntities = baseMapper.selectList(new QueryWrapper<>());
        return indexCalculationTaskEntities.stream().map(IndexCalculationTaskEntity::getCalculationYear).distinct().collect(Collectors.toList());
    }

    @Override
    public Object getCalculationArea() {
        List<IndexCalculationTaskEntity> indexCalculationTaskEntities = baseMapper.selectList(new QueryWrapper<>());
        return indexCalculationTaskEntities.stream().map(IndexCalculationTaskEntity::getCalculationArea).distinct().collect(Collectors.toList());
    }
}
